#!/usr/bin/perl
#<<Perl by Example>> 4th edition. Section 19.1.4.

# Write an awklike report

open( MYDB, "> mydb" ) || die "Can't open mydb: $!\n";
$oldfilehandle = select(MYDB);

# MYDB is selected as the filehandle for write
# format is ended by a single '.' on a seperate line. This
# line can NOT contain any other characters besides '.'.
format MYDB_TOP =
                     DATEBOOK INFO

      Name              Phone         Birthday    Salary
  _________________________________________________________
.

format MYDB =
  @<<<@<<<<<<<<<<<<<<<<@<<<<<<<<<<<<@|||||||||||@#######.##
  $.,      $name,         $phone,       $bd,      $sal
.

format SUMMARY =
  _________________________________________________________
  The average salary for all employees is $@######.##.
                                          $total/$count
  The number of lines left on the page is @###.
                                          $-
  The default page length is @###.
                             $=
.

open( DB, "databook" ) || die "Can't open datebook: $!\n";
while (<DB>)
{
	( $name, $phone, $bd, $sal ) = split(/:/);
	write;
	$count++;
	$total += $sal;
}
close DB;

$~ = SUMMARY;    # New report format for MYDB filehandle
write;

select($oldfilehandle);    # STDOUT is now selected for further
                           # writes or prints
print "Report Submitted On", 'date';
